/**
 * Author(s): xuming.Wong <xuming.Wong@gmail.com>
 */


#ifndef __QS_PortfolioManager_H__
#define __QS_PortfolioManager_H__

#include "Prerequisites.h"
#include "QueryManager.h"

namespace QSThink
{
	class PortfolioManager;

	class PortfolioItem
	{
	public:
		PortfolioItem()
		{
			dateTime=QDateTime::currentDateTime();
			amount=0;
			highPrice=0;
			lowPrice=0;
			openPrice=0;
			lastClosePrice=0;
		}

		QString symbol;
		QDateTime dateTime;//added date time

		int amount;
		float price;//current price
		float highPrice;
		float lowPrice;
		float openPrice;
		float lastClosePrice;

	};


	class PortfolioListener
	{
	public:
		PortfolioListener();
	protected:
		friend class PortfolioManager;
		virtual void OnPortfolioAdded(QString symbol)=0;
		virtual void OnPortfolioRemoved(QString symbol)=0;
		virtual void OnPortfolioUpdated(PortfolioItem* item)=0;
		//virtual void OnSymbolRemoved(QString symbol)=0;

	};

	
	//should have portfolio history management
	//that need every symbol have a history file.
	//only one portfolio now allowed for simple and easy use.
	class PortfolioManager 
		: public QObject,
		public QueryListener
		
	{
		Q_OBJECT
	public:
		static const QString PortfolioFileName;
		friend class PortfolioListener;
	public:
		PortfolioManager();
		static PortfolioManager& getSingleton();

		float getCapital();

		int getSymbolCount();
		QString getSymbol(int index);
		int getAmount(int index);
		int getAmount(QString symbol);

		void setAmount(QString symbol,int amount);
		PortfolioItem* getPortfolioItem( QString symbol );

		//add to your portfolio
		//return the index of the symbol
		//when portfolio get DiscreteTransactionInfo call listener 
		void addSymbol(QString symbol);//using string is because Other Exchange using letter

		void removeSymbol(QString symbol);

	protected:
		void addListener(PortfolioListener* listener);
		void persist();
		void loadPortfolio();
	protected:
		std::vector<PortfolioItem*> _items;
		std::vector<PortfolioListener*> _listeners;
	protected:
		void OnTransactionReceived(DiscreteTransaction* info);
	protected slots:
		void update();
		
	};
}


#endif
